<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *   Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 * 
 */

if ($action == 'login') {
	unset($action);
	include(ROOT_PATH."includes/login.php");
} elseif ($action == 'logout') {
	unset($action);
	include(ROOT_PATH."includes/logout.php");
} else {
	if (!isset($param['rc']) and $action!='reguser' and $action != 'pwdrecovery') {
		if (isset($_COOKIE['poat']['id'])) {
			$_SESSION["s_user"] = $_COOKIE['poat']['id'];		
		}
	}
}

$tm->assign('location',$action);

if (isset($_SESSION['s_user'])) {
	$tm->assign('logued',1);	

	include_once(ROOT_PATH."includes/currentuser.php");
	
	$isadmin = $CURRENT_USER->groupid == GROUP_ADMIN;

	include(ROOT_PATH."includes/prmenu.php");
	
	if (validCredentials($CURRENT_USER,$action)) {

		include_once(ROOT_PATH."includes/messagedisplay.php");
		include_once(ROOT_PATH."includes/taskdisplay.php");

		if ($action!='') {
			include(ROOT_PATH."includes/".$action .".php");
		} else {
			if (!$isadmin) {
				if (SAVE_SESSION == 'true' and isset($_COOKIE['poat'][$CURRENT_USER->userid]['projectid'])) {
					$param['projectid'] = $_COOKIE['poat'][$CURRENT_USER->userid]['projectid'];
					include(ROOT_PATH."includes/list.php");	
				} else {
					include(ROOT_PATH."includes/prselection.php");	
				}
			}
		}	
	} else {
		$error = $LANG['notauth'];
	}
	
} else {
	if (isset($param['rc']) or $action=='chpwd') {
		include(ROOT_PATH."includes/chpwd.php");
	} elseif ($action=='reguser') {
		include(ROOT_PATH."includes/reguser.php");
	} elseif ($action == 'pwdrecovery') {
		include(ROOT_PATH."includes/pwdrecovery.php");	
	} else {
		$tm->assign('location',"");
		$tm->assign('redir',$action);
		include(ROOT_PATH."includes/login.php");
	}
}

?>
